$(function(){
    $('nav#menu_left').mmenu();
    $('.add_rel_tag').on('click', function(e){
        $('#newRelTag').modal('show');
    });
    $('#addBtn').on('click', function(e){
        var tag = $.trim($('#tagContent').val());
        if(tag){
            addTag(tag, userId, function(){
                var html = '<span class="rel_tag" data-val="'+tag+'">'+tag+'</span>';
                $('.add_rel_tag').before(html);
                $('#tagContent').val('');
                $('#newRelTag').modal('hide');
            })
        }
    });
    $('.rel_tag').live('click', function(e){
        var tage = $(this),val = tage.attr('data-val');
        if(tage.find('i').length === 0){
            tage.append('<i class="icon-cross del_tag"></i>')
            tage.css({'background-color':'#5CB85C','color':'#ffffff'});
        }else{
            removeTag(val, userId, function(){
                tage.remove();
            })
        }
    });
    $('#editCard').on('click', function(e){
        window.location.href='/newCard/'+cardId;
    });
    $('#setPrivacy').on('click', function(e){
        window.location.href='/setPrivacy';
    })
    $('#headlogo').on('click', function(e){
        AlertBox.showTip('更新中...');
        $.getJSON('/refresh', function(res){
            window.location.reload();
        });
    });
    $('.sameacts').live('click', function(e){
        window.location.href = '/invite/'+$(this).attr('data-id');
    });
    $('.samefaction').live('click', function(e){
        window.location.href = '/faction/'+$(this).attr('data-id');
    });
    $('#receiveCard').on('click', function(e){
        $.post('/receiveCard', {userId: userId}, function(res){
            if(!res.error){
                if(res.already)
                    return AlertBox.showTip('已在人脉中');
                else
                    AlertBox.showTip('新伙伴加入!');
                window.location.href = '/user/'+userId;
            }
            AlertBox.showTip('抱歉,出错了')
        })
    });
    //weixinShare();
    // 拉取关系信息
    getTwoUserRelInfo();
})
function getTwoUserRelInfo(){
    if(userId === meId || isfriend === 'false') return;
    $.get('/getRelsInfo/'+userId, function(res){
        var factions = res.factions,
            len = factions.length;
        if(len > 0){
            var samef = $('#samefaction'),
                html = '';
            for(var i=0; i<len; i++){
                html += '<span class="badge faction_badge samefaction" data-id="'+factions[i].id+'">'+factions[i].name+'</span> '
            }
            samef.append(html);
            samef.parent().fadeIn();
        }
        var acts = res.acts,
            actlen = acts.length;
        if(actlen > 0){
            var samea = $('#sameact'),
                html = '';
            for(var j=0; j<actlen; j++){
                html += '<span class="label label-primary sameacts" data-id="'+acts[j]._id+'">'+acts[j].name+'</span> ';
            }
            samea.append(html);
            samea.parent().fadeIn();
        }
    })
}
function addTag(tag, userId, callback){
    $.post('/addTag', {userId: userId, tag: tag}, function(res){
        if(res.error){
            AlertBox.showTip('出问题了T_T')
        }else{
            if(res.data === 'none'){
                AlertBox.showTip('先收下他的名片吧')
            }else{
                callback();
            }
        }
    })
}
function removeTag(tag, userId, callback){
    $.post('/removeTag', {userId: userId, tag: tag}, function(res){
        if(res.error){
            AlertBox.showTip('出问题了T_T')
        }else{
            callback();
        }
    })
}